package com.evildevil.engines.bubble.texture;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import zombie.core.textures.Texture;
import zombie.core.textures.TexturePackPage;
import zombie.debug.DebugLog;

/* loaded from: DDSLoader.class */
public final class DDSLoader implements DDSurface {
    private final String DDS_IDENTIFIER = "DDS ";
    private final int DDS_HEADER_SIZE = 128;
    private final int DDS_DESC2_RESERVED_1 = 44;
    private final int DDS_DESC2_RESERVED_2 = 4;
    private final int DDS_CAPS2_RESERVED = 8;
    private final int DEFAULT_DXT_BLOCKSIZE = 16;
    private final int DXT1_BLOCKSIZE = 8;
    private final DDSurfaceDesc2 ddsDesc2 = new DDSurfaceDesc2();
    private BufferedInputStream ddsFileChannel = null;
    private static ByteBuffer ddsHeader = null;
    static ByteBuffer imageData = null;
    static ByteBuffer imageData2 = null;
    public static int lastWid = 0;
    public static int lastHei = 0;

    public int loadDDSFile(String str) {
        try {
            new FileInputStream(new File(str));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (this.ddsFileChannel == null) {
            throw new NullPointerException("ddsFileChannel couldn't be null!");
        }
        if (ddsHeader == null) {
            ddsHeader = ByteBuffer.allocate(128);
        }
        readFileHeader();
        return readFileData();
    }

    public int loadDDSFile(BufferedInputStream bufferedInputStream) {
        this.ddsFileChannel = bufferedInputStream;
        if (this.ddsFileChannel == null) {
            throw new NullPointerException("ddsFileChannel couldn't be null!");
        }
        if (ddsHeader == null) {
            ddsHeader = ByteBuffer.allocate(128);
        }
        ddsHeader.rewind();
        readFileHeader();
        return readFileData();
    }

    private void readFileHeader() {
        try {
            try {
                this.ddsFileChannel.read(ddsHeader.array());
                this.ddsDesc2.setIdentifier(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                this.ddsDesc2.setSize(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                this.ddsDesc2.setFlags(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                this.ddsDesc2.setHeight(TexturePackPage.readInt(ddsHeader));
                this.ddsDesc2.setWidth(TexturePackPage.readInt(ddsHeader));
                this.ddsDesc2.setPitchOrLinearSize(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                this.ddsDesc2.setDepth(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                this.ddsDesc2.setMipMapCount(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                ddsHeader.position(ddsHeader.position() + 44);
                DDPixelFormat dDPixelformat = this.ddsDesc2.getDDPixelformat();
                dDPixelformat.setSize(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setFlags(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setFourCC(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setRGBBitCount(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setRBitMask(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setGBitMask(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setBBitMask(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDPixelformat.setRGBAlphaBitMask(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                DDSCaps2 dDSCaps2 = this.ddsDesc2.getDDSCaps2();
                dDSCaps2.setCaps1(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                dDSCaps2.setCaps2(TexturePackPage.readInt(ddsHeader) & 4294967295L);
                ddsHeader.position(ddsHeader.position() + 8);
                ddsHeader.position(ddsHeader.position() + 4);
            } catch (TextureFormatException e) {
                e.printStackTrace();
            } catch (BufferUnderflowException e2) {
                e2.printStackTrace();
            }
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }

    private int readFileData() {
        DDPixelFormat dDPixelformat = this.ddsDesc2.getDDPixelformat();
        int i = 0;
        if (dDPixelformat.isCompressed && dDPixelformat.getFourCCString().equalsIgnoreCase("DXT1")) {
            calculateSize(8);
            i = 33777;
        } else {
            calculateSize(16);
            if (dDPixelformat.getFourCCString().equalsIgnoreCase("DXT3")) {
                i = 33778;
            } else if (dDPixelformat.getFourCCString().equals("DXT5")) {
                i = 33779;
            }
        }
        if (imageData == null) {
            imageData = ByteBuffer.allocate(DDSurface.DDSCAPS_MIPMAP);
        }
        imageData.rewind();
        try {
            this.ddsFileChannel.read(imageData.array(), 0, (int) this.ddsDesc2.pitchOrLinearSize);
            imageData.flip();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (imageData2 == null) {
            imageData2 = ByteBuffer.allocateDirect(DDSurface.DDSCAPS_MIPMAP);
        }
        imageData2.clear();
        imageData2.put(imageData.array(), 0, (int) this.ddsDesc2.pitchOrLinearSize);
        imageData2.flip();
        lastWid = (int) this.ddsDesc2.width;
        lastHei = (int) this.ddsDesc2.height;
        int glGenTextures = GL11.glGenTextures();
        Texture.lastTextureID = glGenTextures;
        GL11.glBindTexture(3553, glGenTextures);
        GL11.glTexParameteri(3553, 10240, 9728);
        GL11.glTexParameteri(3553, 10241, 9728);
        GL11.glTexParameteri(3553, 10242, 10497);
        GL11.glTexParameteri(3553, 10243, 10497);
        GL13.glCompressedTexImage2D(3553, 0, i, (int) this.ddsDesc2.width, (int) this.ddsDesc2.height, 0, imageData2);
        return glGenTextures;
    }

    private int calculateSize(int i) {
        return (int) (Math.ceil(this.ddsDesc2.width / 4) * Math.ceil(this.ddsDesc2.height / 4) * i);
    }

    public void debugInfo() {
        DDPixelFormat dDPixelformat = this.ddsDesc2.getDDPixelformat();
        DDSCaps2 dDSCaps2 = this.ddsDesc2.getDDSCaps2();
        DebugLog.log("\nDDSURFACEDESC2:");
        DebugLog.log("----------------------------------------");
        DebugLog.log("SIZE: " + this.ddsDesc2.size);
        DebugLog.log("FLAGS: " + this.ddsDesc2.flags);
        DebugLog.log("HEIGHT: " + this.ddsDesc2.height);
        DebugLog.log("WIDTH: " + this.ddsDesc2.width);
        DebugLog.log("PITCH_OR_LINEAR_SIZE: " + this.ddsDesc2.pitchOrLinearSize);
        DebugLog.log("DEPTH: " + this.ddsDesc2.depth);
        DebugLog.log("MIP_MAP_COUNT: " + this.ddsDesc2.mipMapCount);
        DebugLog.log("\nDDPIXELFORMAT of DDSURFACEDESC2:");
        DebugLog.log("----------------------------------------");
        DebugLog.log("SIZE :" + dDPixelformat.size);
        DebugLog.log("FLAGS: " + dDPixelformat.flags);
        DebugLog.log("FOUR_CC: " + dDPixelformat.getFourCCString());
        DebugLog.log("RGB_BIT_COUNT: " + dDPixelformat.rgbBitCount);
        DebugLog.log("R_BIT_MASK: " + dDPixelformat.rBitMask);
        DebugLog.log("G_BIT_MASK: " + dDPixelformat.gBitMask);
        DebugLog.log("B_BIT_MASK: " + dDPixelformat.bBitMask);
        DebugLog.log("RGB_ALPHA_BIT_MASK: " + dDPixelformat.rgbAlphaBitMask);
        DebugLog.log("\nDDSCAPS of DDSURFACEDESC2");
        DebugLog.log("----------------------------------------");
        DebugLog.log("CAPS1: " + dDSCaps2.caps1);
        DebugLog.log("CAPS2: " + dDSCaps2.caps2);
    }
}